package com.windoent.yugioh.ui.activity.registlogin;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.baidu.mobstat.StatService;
import com.windoent.yugioh.AppManager;
import com.windoent.yugioh.MainActivity;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.bean.GeneralBean;
import com.windoent.yugioh.constants.Constants;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.LogUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import okhttp3.Call;

/**
 * author : robert
 * date   : 2021/4/28 13:53
 * desc   :登录注册绑定手机号
 */
public class BindingCellPhoneActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout mLlJump;
    private EditText mEtCellphone;
    private ImageView mIvDelete;
    private Button mBtnGetCode;
    private int third_type;
    private String auth_code;
    private String access_token;
    private String openid;
    private View mView;


    @Override
    public int getLayout() {
        return R.layout.activity_binding_cell_phone;
    }

    @Override
    public void initView() {
        super.initView();
        AppManager.getAppManager().addActivity(this);
        mView = (View) findViewById(R.id.view);
        immersiveStatusBar(mView);

        mLlJump = (LinearLayout) findViewById(R.id.ll_jump);
        mEtCellphone = (EditText) findViewById(R.id.et_cellphone);
        mIvDelete = (ImageView) findViewById(R.id.iv_delete);
        mBtnGetCode = (Button) findViewById(R.id.btn_get_code);

    }

    @Override
    public void initData() {
        super.initData();
        mIvDelete.setVisibility(View.GONE);
        Intent intent = getIntent();
        third_type = intent.getIntExtra(Constants.THIRD_TYPE, -1);
        auth_code = intent.getStringExtra("auth_code");
        access_token = intent.getStringExtra("access_token");
        openid = intent.getStringExtra("openid");
    }

    @Override
    public void initListener() {
        super.initListener();
        mLlJump.setOnClickListener(this);
        mEtCellphone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                mIvDelete.setVisibility(BaseMethod.notNull(charSequence.toString().trim()) && charSequence.toString().trim().length() > 0 ? View.VISIBLE : View.GONE);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        mIvDelete.setOnClickListener(this);
        mBtnGetCode.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.ll_jump:
                startActivity(new Intent(BindingCellPhoneActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.iv_delete:
                mEtCellphone.setText("");
                break;
            case R.id.btn_get_code:
                String s = mEtCellphone.getText().toString().trim();
                if (BaseMethod.notNull(s) && s.length() > 0) {
                    getCellPhoneCode(s);
                } else {
                    ToastUtils.showText("请输入您的手机号");
                }
                break;
            default:
                break;
        }
    }

    //获取手机号验证码
    private void getCellPhoneCode(String cell) {
        showLoadingDialog("");
        PhoneLive.getCellPhoneCode(cell, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                LogUtils.d("response-->" + response);
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            GeneralBean generalBean = gson.fromJson(response, GeneralBean.class);
                            ToastUtils.showText(generalBean.getMsg());
                            Intent intent = new Intent(mContext, BindingCellPhoneCodeActivity.class);
                            intent.putExtra("bind_cell", cell);
                            if (third_type == 0) {
                                intent.putExtra("auth_code", auth_code);
                                intent.putExtra(Constants.THIRD_TYPE, third_type);
                            } else if (third_type == 1) {
                                intent.putExtra(Constants.THIRD_TYPE, third_type);
                                intent.putExtra("access_token", access_token);
                                intent.putExtra("openid", openid);
                            }
                            startActivity(intent);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getString(R.string.string_response_error));
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "登录注册绑定手机号界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "登录注册绑定手机号界面");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }
}
